/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React from 'react';
import { Space } from 'antd';
import { QuestionCircleOutlined } from '@ant-design/icons';
import type { ProColumns } from '@ant-design/pro-table';
import KbSwitch from './components/KbSwitch';
import KbCollectionModal from './components/KbCollectionModal';
import KbSmsCloudCall from './components/KbSmsCloudCall';
import KbEditTable from './components/KbSmsCloudCall/components/KbEditTable';
import KbCloudCallSwitch from './components/KbSmsCloudCall/components/KbCloudCallSwitch';
import type { TableListItem, COLUMNS, CLOUDCALLCOLUMNS, DataSourceType } from './index.d';
import { loadData } from './utils';
import moment from 'moment';

export interface DataType {
  key: React.Key;
  name: string;
  chinese: number;
  math: number;
  english: number;
}

const siteOptions = [
  {
    label: '全部',
    values: '全部',
  },
  {
    label: '社区',
    values: '社区',
  },
  {
    label: '校园',
    values: '校园',
  },
  {
    label: '办公区',
    values: '办公区',
  },
  {
    label: '工业园区',
    values: '工业园区',
  },
  {
    label: '商业街',
    values: '商业街',
  },
  {
    label: '批发市场',
    values: '批发市场',
  },
  {
    label: '乡镇',
    values: '批发市场',
  },
  {
    label: '村庄',
    values: '批发市场',
  },
];
const defOptions = [
  {
    label: '全部',
    value: '0',
  },
  {
    label: '去0',
    value: '1',
  },
  {
    label: '大于0小于等于100',
    value: '2',
  },
  {
    label: '大于100',
    value: '3',
  },
];

const addressOptions = [
  {
    label: '全部',
    value: '',
  },
  {
    label: '有地址',
    value: '1',
  },
  {
    label: '无地址',
    value: '0',
  },
];

const placeholderList = {
  siteSearch: '驿站id/名称/联系电话/地址关键词搜索',
  codeSearch: '请输入编码或驿站名称/账号',
};

function getColumns({ activeKey, options, setOptions, onChange, actionRef }: COLUMNS) {
  const columns: ProColumns<TableListItem>[] = [
    {
      title: '',
      dataIndex: 'cascader',
      valueType: 'cascader',
      hideInTable: true,
      hideInSearch: activeKey !== 'siteSearch',
      fieldProps: {
        options: options,
        loadData: (selectedOptions: any) => loadData(selectedOptions, options, setOptions),
        onChange: onChange,
        changeOnSelect: true,
        placeholder: '请选择省市区',
      },
    },
    {
      title: '驿站类型',
      dataIndex: 'area_type',
      valueType: 'select',
      initialValue: '全部',
      hideInTable: true,
      hideInSearch: activeKey !== 'siteSearch',
      fieldProps: { options: siteOptions, allowClear: false },
    },
    {
      hideInTable: true,
      dataIndex: 'keywords',
      hideInSearch: activeKey === 'closeSite',
      fieldProps: { placeholder: placeholderList[activeKey] },
    },
    {
      title: '昨日入库量',
      dataIndex: 'input_filter',
      valueType: 'select',
      initialValue: '0',
      hideInTable: true,
      hideInSearch: activeKey !== 'siteSearch',
      fieldProps: { options: defOptions, allowClear: false },
    },
    {
      title: '注册日期',
      dataIndex: 'create_at',
      valueType: 'dateRange',
      hideInTable: true,
      hideInSearch: activeKey !== 'siteSearch',
      fieldProps: {
        placeholder: ['不限制', '不限制'],
        allowClear: true,
        disabledDate: (current) => current && current.isAfter(moment(), 'day'),
      },
    },
    {
      title: '驿站地址',
      dataIndex: 'has_concat_location',
      valueType: 'select',
      hideInTable: true,
      hideInSearch: activeKey !== 'siteSearch',
      fieldProps: { options: addressOptions, allowClear: false, placeholder: '全部' },
    },
    {
      title: '驿站id',
      dataIndex: 'dak_id',
      align: 'center',
      search: false,
      width: 80,
      ellipsis: true,
    },
    {
      title: '驿站名称',
      dataIndex: 'dak_name',
      align: 'center',
      search: false,
      width: 120,
      ellipsis: true,
    },
    {
      title: '驿站账号',
      dataIndex: 'mobile',
      align: 'center',
      search: false,
      ellipsis: true,
      width: 120,
      render: (_, record) => {
        const { master_phone, phone } = record;
        return master_phone ? master_phone : phone;
      },
    },
    {
      title: '编码',
      dataIndex: 'yt_station_code',
      hideInTable: activeKey !== 'codeSearch', //编码
      align: 'center',
      search: false,
      ellipsis: true,
      width: 80,
    },
    {
      title: '联系电话',
      dataIndex: 'concat_phone',
      align: 'center',
      search: false,
      ellipsis: true,
      width: 120,
    },
    {
      title: '注册时间',
      dataIndex: 'create_at',
      align: 'center',
      search: false,
      ellipsis: true,
      width: 170,
    },
    {
      title: '驿站地址',
      dataIndex: 'address',
      align: 'center',
      width: 120,
      search: false,
    },
    {
      title: '区域类型',
      dataIndex: 'area_type',
      align: 'center',
      search: false,
      width: 120,
      ellipsis: true,
    },
    {
      title: '加盟商',
      dataIndex: 'isJoined',
      align: 'center',
      search: false,
      width: 120,
      render: (_, record) => {
        const { isJoined, join_inn_name } = record;
        return (
          <Space direction='vertical'>
            <span>{isJoined ? '已加盟' : '未加盟'}</span>
            <span>{join_inn_name}</span>
          </Space>
        );
      },
    },
    {
      title: '昨日入库量',
      dataIndex: 'ytd_num',
      align: 'center',
      search: false,
      width: 100,
      ellipsis: true,
      sorter: true,
    },
    {
      title: '登录',
      dataIndex: 'is_login',
      align: 'center',
      search: false,
      width: 60,
      ellipsis: true,
      hideInTable: activeKey === 'rejected',
      render: (_, record) => {
        return <KbSwitch type='is_login' record={record} actionRef={actionRef} />;
      },
    },
    {
      title: '入库',
      dataIndex: 'is_pass',
      align: 'center',
      search: false,
      width: 60,
      ellipsis: true,
      hideInTable: activeKey === 'rejected',
      render: (_, record) => {
        return <KbSwitch type='is_pass' record={record} actionRef={actionRef} />;
      },
    },
    {
      title: '提现',
      dataIndex: 'is_withdraw',
      align: 'center',
      search: false,
      width: 60,
      ellipsis: true,
      hideInTable: activeKey === 'rejected',
      render: (_, record) => {
        return <KbSwitch type='is_withdraw' record={record} actionRef={actionRef} />;
      },
    },
    {
      title: '驿站管理',
      dataIndex: 'area_type',
      align: 'center',
      search: false,
      width: 90,
      ellipsis: true,
      hideInTable: activeKey === 'rejected',
      render: (_, record) => {
        return (
          <Space direction='vertical'>
            <KbCollectionModal item={record}>
              <a>代收</a>
            </KbCollectionModal>
            <KbSmsCloudCall item={record}>
              <a>短信云呼</a>
            </KbSmsCloudCall>
          </Space>
        );
      },
    },
  ];
  return columns;
}

function getCloudCallColumns({ onSave, onSwitch }: CLOUDCALLCOLUMNS) {
  const cloudCallColumns: ProColumns<DataSourceType>[] = [
    {
      title: '（仅入库）不通知',
      dataIndex: 'only_sms_auth',
      valueType: 'text',
      tooltip: {
        icon: <QuestionCircleOutlined />,
        title: '开启后，开通代收的品牌也可入库不发短信，可单号入库',
      },
      width: '33%',
      align: 'center',
      render: (_, record) => {
        return (
          <KbCloudCallSwitch status={record.only_sms_auth} record={record} onSwitch={onSwitch} />
        );
      },
    },
    {
      title: '暂不通知比例',
      dataIndex: 'notice_percentage',
      width: '33%',
      align: 'center',
      tooltip: {
        icon: <QuestionCircleOutlined />,
        title: '每天暂不通知数量=前一周日均入库量*该比例',
      },
      fieldProps: {
        min: 0,
        max: 100,
        placeholder: '输入1～100之间的整数',
        width: 100,
      },
      render: (_, record) => {
        return <KbEditTable onSave={onSave} value={record.notice_percentage} />;
      },
    },
  ];
  return cloudCallColumns;
}

export { getColumns, getCloudCallColumns };
